<?php

namespace App\Services;

class FbService
{

    public function rebuildCookie($oldCookie)
    {
        parse_str(str_replace(';', '&', $oldCookie), $cookieArr);
        $newCookieArr = [];
        foreach ($cookieArr as $name => $value) {
            if (in_array($name, ['xs', 'sb', 'spin', 'fr', 'datr'])) {
                $newCookieArr[] =  $this->defaultValue($name,$value,true,true);
            }
            if (in_array($name, ['wd', 'c_user'])) {
                $newCookieArr[] =  $this->defaultValue($name,$value,false,true);
            }
        }
        return json_encode($newCookieArr,JSON_UNESCAPED_SLASHES);
    }

    protected function defaultValue($name, $value, $httpOnly = false, $secure = false)
    {
        $cookieArray['domain'] = '.facebook.com';
        $cookieArray['expiry'] = intval(microtime(true)*1000 + 60 * 60 * 24 * 365 * 1000);
        $cookieArray['httpOnly'] = $httpOnly;
        $cookieArray['name'] = $name;
        $cookieArray['path'] = '/';
        $cookieArray['priority'] = 'Medium';
        $cookieArray['sameParty'] = false;
        $cookieArray['secure'] = $secure;
        $cookieArray['session'] = false;
        $cookieArray['size'] = strlen($value);
        $cookieArray['sourcePort'] = '443';
        $cookieArray['sourceScheme'] = 'Secure';
        $cookieArray['value'] = $value;
        return $cookieArray;
    }
}
